package util.core.plugins.mycraft.cc;

import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import net.sf.cglib.beans.*;

public class DynamicBean {
	public Object object = null;
	// 属性map
	public BeanMap beanMap = null;

	public DynamicBean() {
		super();
	}

	@SuppressWarnings("rawtypes")
	public DynamicBean(Map<String, Class> propertyMap, Object obj) {
		// 用一组属性生成实体Bean
		this.object = generateBean(propertyMap, obj);
		// 用实体Bean创建BeanMap，以便可以设置和获取Bean属性的值
		this.beanMap = BeanMap.create(this.object);
	}

	/**
	 * 给bean中的属性赋值
	 * 
	 * @param property
	 *            属性名
	 * @param value
	 *            值
	 */
	public void setValue(String property, Object value) {
		beanMap.put(property, value);
	}

	/**
	 * 获取bean中属性的值
	 * 
	 * @param property
	 *            属性名
	 * @return 值
	 */
	public Object getValue(String property) {
		return beanMap.get(property);
	}

	/**
	 * 得到该实体bean对象
	 * 
	 * @return
	 */
	public Object getObject() {
		return this.object;
	}

	@SuppressWarnings("rawtypes")
	private Object generateBean(Map<String, Class> propertyMap, Object obj) {
		// 根据一组属性名和属性值的类型，动态创建Bean对象
		BeanGenerator generator = new BeanGenerator();
		generator.setSuperclass(obj.getClass());  // 设置父类
		Set keySet = propertyMap.keySet();
		for (Iterator i = keySet.iterator(); i.hasNext();) {
			String key = (String) i.next();
			generator.addProperty(key, (Class) propertyMap.get(key));
		}
		return generator.create(); // 创建Bean
	}

}